package at.ac.tuwien.ifs.calendit.basis.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

import at.ac.tuwien.ifs.calendit.basis.helper.Constants;

import com.toedter.calendar.JDateChooser;


public final class Wochenansicht extends JPanel {
	  /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static JTable __table;
	private JScrollPane __scrollPane;
	private JPanel top;
    public static JCheckBox privat_chb;
    public static JCheckBox arbeits_chb;
    private JLabel arbeits_lbl;
    private JLabel privat_lbl;
    private JButton ok_butt;
    public static JDateChooser cal;
    private TableCellRenderer defaultRenderer;
    private Date d;
 
	  public Wochenansicht() {
	    super();
	    privat_lbl = new JLabel ("Private Termine");
        arbeits_lbl = new JLabel ("Arbeits-Termine");
        privat_chb = new JCheckBox();
        arbeits_chb = new JCheckBox();      
        privat_chb.setBackground(Color.white);
        arbeits_chb.setBackground(Color.white);
        privat_chb.setSelected(true);
        arbeits_chb.setSelected(true);
        cal= new JDateChooser();
        cal.setDate(Constants.selectedDate);
        
        privat_chb.addActionListener(new Privatchb_Action());
        arbeits_chb.addActionListener(new Arbeitchb_Action());

        __table = new JTable(new JWochenAnsichtTableModel());
	    defaultRenderer = __table.getDefaultRenderer(JComponent.class);
	    __table.setDefaultRenderer(JComponent.class,
				       new JComponentButtonRenderer(defaultRenderer));
	    TableColumn col = __table.getColumnModel().getColumn(0);
	    col.setPreferredWidth(80);
	    col.setMinWidth(80);
	    col.setMaxWidth(110); 
	    
	    //Register Listener
	    __table.setPreferredScrollableViewportSize(new Dimension(400, 200));
	    __table.addMouseListener(new JWochenAnsichtMouseListener(__table));
	    __table.getTableHeader().setResizingAllowed(true);
	    __table.getTableHeader().setReorderingAllowed(false);
	    __table.setRowSelectionAllowed(false);
	    __table.setColumnSelectionAllowed(false);
	    __table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

	    __scrollPane = new JScrollPane(__table);
	    this.setLayout(new BorderLayout());
	    this.setBorder(BorderFactory.createTitledBorder("Wochenansicht"));
	    this.setBackground(Color.white);
	    
	 // Add controls 
        top = new JPanel();
        top.setBackground(Color.WHITE);
        {
        	ok_butt = new JButton();
        	top.add(ok_butt);
        	ok_butt.setBorderPainted(false);
        	ok_butt.setBackground(Color.white);
        	ok_butt.setIcon(Constants.ICON_OK);
        	ok_butt.setPressedIcon(Constants.ICON_OK_pressed);
        	ok_butt.setPreferredSize(new Dimension(21,21));
        	ok_butt.addActionListener(new OKButton_Action2());
        }
        top.add(cal);

        top.add(arbeits_chb);
        top.add(arbeits_lbl);
        top.add(privat_chb);
        top.add(privat_lbl);

        this.add(top, BorderLayout.NORTH);
	    this.add(__scrollPane,BorderLayout.CENTER);
	    this.repaint();
	  }

	  
	  private class OKButton_Action2 implements ActionListener{

			public void actionPerformed(ActionEvent e) {

				 d= new Date(cal.getDate().getYear(), cal.getDate().getMonth(), cal.getDate().getDate());
				 Constants.selectedDate=d;
				 JWochenAnsichtTableModel.jt.setText(Integer.toString(cal.getDate().getDay()));
				 JWochenAnsichtTableModel.refreshColumn();

				 __table.getColumnModel().getColumn(1).setHeaderValue("<html><b>"+JWochenAnsichtTableModel.weekdates[0].getDate()+"</b>&nbsp;&nbsp;&nbsp;&nbsp;"+"MO");
				 __table.getColumnModel().getColumn(2).setHeaderValue("<html><b>"+JWochenAnsichtTableModel.weekdates[1].getDate()+"</b>&nbsp;&nbsp;&nbsp;&nbsp;"+"DI");
				 __table.getColumnModel().getColumn(3).setHeaderValue("<html><b>"+JWochenAnsichtTableModel.weekdates[2].getDate()+"</b>&nbsp;&nbsp;&nbsp;&nbsp;"+"MI");
				 __table.getColumnModel().getColumn(4).setHeaderValue("<html><b>"+JWochenAnsichtTableModel.weekdates[3].getDate()+"</b>&nbsp;&nbsp;&nbsp;&nbsp;"+"D0");
				 __table.getColumnModel().getColumn(5).setHeaderValue("<html><b>"+JWochenAnsichtTableModel.weekdates[4].getDate()+"</b>&nbsp;&nbsp;&nbsp;&nbsp;"+"FR");
				 __table.getColumnModel().getColumn(6).setHeaderValue("<html><b>"+JWochenAnsichtTableModel.weekdates[5].getDate()+"</b>&nbsp;&nbsp;&nbsp;&nbsp;"+"SA");
				 __table.getColumnModel().getColumn(7).setHeaderValue("<html><b>"+JWochenAnsichtTableModel.weekdates[6].getDate()+"</b>&nbsp;&nbsp;&nbsp;&nbsp;"+"SO");
				    repaint();
				    KalenderUebersicht.reloadTables();
			}
	    	
	    }
	  
	  private class Arbeitchb_Action implements ActionListener{

			public void actionPerformed(ActionEvent e) {
				d= new Date(cal.getDate().getYear(), cal.getDate().getMonth(), cal.getDate().getDate());
				 Constants.selectedDate=d;
				 JWochenAnsichtTableModel.jt.setText(Integer.toString(cal.getDate().getDay()));
						 __table.repaint();
						 String cmd= e.getActionCommand();
								__table.repaint();

			}
	    	
	    }
	  private class Privatchb_Action implements ActionListener{

			public void actionPerformed(ActionEvent e) {
				d= new Date(cal.getDate().getYear(), cal.getDate().getMonth(), cal.getDate().getDate());
				 Constants.selectedDate=d;
				 JWochenAnsichtTableModel.jt.setText(cal.getDate().getDay()+"privat");
				 __table.repaint();
			}
	    }
	}
